//
//  SettingPageController.m
//  O2Cat
//
//  Created by xiabin on 15/12/16.
//  Copyright © 2015年 QiDao.Purifier. All rights reserved.
//

#import "SettingPageController.h"
#import "RegisterViewController.h"
#import "DeviceListPageController.h"
#import "DeviceInfoPageController.h"
#import "ProfilePageController.h"

#define sepH 10

@interface SettingPageController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *sectionItems;
    NSDictionary *sectionItemsIcon;
    NSArray      *sectionKeys;
    int HeaderHigh;
    int bLogin;
}

@property (strong, nonatomic) UITableView *settingTableView;
@property (strong, nonatomic) UIView *headerView;
@property (nonatomic, strong) UIImageView* userHead;
@property (nonatomic, strong) UILabel* userNickLabel;
@property (nonatomic, strong) UILabel* vipLabel;
@property (nonatomic, strong) UIImageView* logoImage;
@property (nonatomic, strong) UIButton* logoutBtn;

@end

@implementation SettingPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = QGPageBG;
    
    HeaderHigh = 90;
    sectionItems = [NSDictionary dictionaryWithObjectsAndKeys:
                    @[@"设备列表",@"设备信息"], @"S1",
                    @[@"关于奇道", @"使用手册", @"售后服务"], @"S2",
                    nil];
    
    sectionItemsIcon = [NSDictionary dictionaryWithObjectsAndKeys:
                        @[@"devices",@"infos"], @"S1",
                        @[@"about", @"handbook", @"sevice"], @"S2",
                        nil];
    
    sectionKeys =  @[@"S1", @"S2"];
    
    [self.view addSubview:self.settingTableView];
    [self.view addSubview:self.logoImage];
    [self.headerView addSubview:self.userHead];
    [self.headerView addSubview:self.vipLabel];
    [self.headerView addSubview:self.userNickLabel];
    [self.view addSubview:self.logoutBtn];

    [self layoutSubViews];
    // Do any additional setup after loading the view.
}

#pragma mark SysCtrl Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sectionKeys count] + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) return 1;
    return [[sectionItems objectForKey:[sectionKeys objectAtIndex:section-1]] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
        static NSString *cellIdentifier2 = @"SettingHeadCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        [cell.contentView addSubview: self.headerView];
        return cell;
    
    }
    
    static NSString *cellIdentifier = @"SettingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    cell.textLabel.text = [[sectionItems objectForKey:[sectionKeys objectAtIndex:indexPath.section-1]] objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[[sectionItemsIcon objectForKey:[sectionKeys objectAtIndex:indexPath.section-1]] objectAtIndex:indexPath.row]];
    cell.textLabel.font = [UIFont systemFontOfSize:[Utils phoneFontL3]];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            [self doLogin];
            break;
        case 1: {
            if (indexPath.row == 0) {
                DeviceListPageController* dvpage = [DeviceListPageController new];
                [self.navigationController pushViewController:dvpage animated:YES];
            } else {
                DeviceInfoPageController* dvpage = [DeviceInfoPageController new];
                [self.navigationController pushViewController:dvpage animated:YES];
            }
        }
        case 2:
            if (indexPath.row == 0) {

            } else if (indexPath.row == 1) {

            }
            break;
        case 3:
            if (indexPath.row == 0) {

            } else if (indexPath.row == 1) {

            }
            break;
        case 4:
            if (indexPath.row == 0) {

            }
            break;
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) return HeaderHigh;
    return 55;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return sepH;
    }
}

-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    } else {
        UIView *sectionsView = [UIView new];
        sectionsView.backgroundColor = QGPageBG;
        return sectionsView;
    }
}



-(void) layoutSubViews {
    int padding = 15;
    //顶部排布
    __weak __typeof(&*self)weakSelf = self;
    
    [self.userHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.headerView.mas_left).offset(padding);
        make.top.mas_equalTo(weakSelf.headerView.mas_top).offset(padding);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(50);
    }];
    
    [self.userNickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.userHead.mas_right).offset(padding);
        make.top.mas_equalTo(weakSelf.userHead.mas_top);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(weakSelf.headerView.mas_width).offset(-100);
    }];
    
    [self.vipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.userHead.mas_right).offset(padding);
        make.top.mas_equalTo(weakSelf.userNickLabel.mas_bottom).offset(padding);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(weakSelf.headerView.mas_width).offset(-100);
    }];
    
    [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).offset(-30);
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX).offset(0);
    }];
    
    [self.logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.settingTableView.mas_bottom).offset(0);
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX).offset(0);
        make.width.mas_equalTo(SCREEN_SIZE_WIDTH-40);
        make.height.mas_equalTo(50);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) viewWillAppear:(BOOL)animated {
    [self setupNavStyle];
}

- (void) setupNavStyle {
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 20)];
    statusBarView.backgroundColor=QGNavBG;
    [self.view addSubview:statusBarView];
    
    self.navigationController.navigationBar.backgroundColor = QGNavBG;
    self.navigationController.navigationBar.barTintColor = QGNavBG;

    self.navigationItem.title = @"奇道空气净化";
    UIButton* backBtn = [Utils NavButtonStyleNormalBLeft:YES NormalImg:@"back" AndHightLight:@"back"];
    [backBtn addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

-(void) popBack {
    [self.navigationController popViewControllerAnimated:NO];
}

-(void) doLogin {
    //RegisterViewController* regpage = [RegisterViewController new];
    //[self.navigationController pushViewController:regpage animated:YES];
    ProfilePageController* profilePage = [ProfilePageController new];
    [self.navigationController pushViewController:profilePage animated:YES];
}

-(void) doLogout {
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark getter & setter

-(UITableView*) settingTableView {
    if (_settingTableView == nil) {
        _settingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 1, self.view.frame.size.width, self.view.frame.size.height - 100 - NavH)];
        _settingTableView.backgroundColor = QGPageBG;
        _settingTableView.delegate = self;
        _settingTableView.dataSource = self;
        _settingTableView.allowsSelection = YES;
        _settingTableView.scrollEnabled = NO;
        _settingTableView.tableFooterView = [UIView new];
        //_settingTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _settingTableView.separatorColor = QGPageBG;

    }
    return _settingTableView;
}

-(UIView *)headerView {
    if (_headerView == nil) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HeaderHigh)];
        _headerView.backgroundColor = [UIColor whiteColor];
        _headerView.userInteractionEnabled = YES;
        UITapGestureRecognizer*tapRecognizerleft=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doLogin)];
        [_headerView addGestureRecognizer:tapRecognizerleft];
    }
    return _headerView;
}

-(UIImageView*) userHead {
    if (_userHead == nil) {
        _userHead = [[UIImageView alloc] init];
        [_userHead.layer setCornerRadius:25];
        [_userHead setContentMode:UIViewContentModeScaleAspectFill];
        [_userHead setClipsToBounds:YES];
        _userHead.layer.borderWidth = 1;
        _userHead.layer.borderColor = QGTextGrayL1.CGColor;
    }
    return _userHead;
}

-(UILabel *)userNickLabel {
    if (_userNickLabel == nil) {
        _userNickLabel = [[UILabel alloc] init];
        _userNickLabel.font = [UIFont systemFontOfSize: [Utils phoneFontL4]];
        _userNickLabel.textColor = QGTextBlackL1;
        _userNickLabel.text = @"用户昵称";
        [_userNickLabel sizeToFit];
    }
    return _userNickLabel;
}

-(UILabel *)vipLabel {
    if (_vipLabel == nil) {
        _vipLabel = [[UILabel alloc] init];
        _vipLabel.font = [UIFont systemFontOfSize: [Utils phoneFontL4]];
        _vipLabel.textColor = QGTextBlackL1;
        _vipLabel.text = @"VIP会员等级";
        [_vipLabel sizeToFit];
    }
    return _vipLabel;
}

-(UIImageView*) logoImage {
    if (_logoImage == nil) {
        _logoImage = [[UIImageView alloc] init];
        _logoImage.image = [UIImage imageNamed:@"deslabel"];
    }
    return _logoImage;
}


-(UIButton*) logoutBtn {
    if(_logoutBtn == nil) {
        _logoutBtn = [[UIButton alloc] init];
        _logoutBtn.backgroundColor = QGNavBG;
        /*_logoutBtn.layer.borderWidth = 1;
        _logoutBtn.layer.borderColor = QGNavBG.CGColor;*/
        _logoutBtn.layer.cornerRadius = 4;
        [_logoutBtn setTitle:@"退出" forState:UIControlStateNormal];
        [_logoutBtn setTitleColor:QGTextWhite forState:UIControlStateNormal];
        [_logoutBtn setTitleColor:QGTextGrayL1 forState:UIControlStateHighlighted];
        [_logoutBtn addTarget:self action:@selector(doLogout) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutBtn;
}
@end
